import pygame
from MatrixUtils import *
from numpy import *
from Constants import *

class Minion:
    def __init__(self, team, position=[100,100], currentHeading=[1,0], currentMovement=[1,0]):
        self.team = team
        self.position = position
        self.radius = MINION_RADIUS
        self.currentHeading = currentHeading
        self.currentMovement = currentMovement
        self.currentRotation = [0,0]
        self.maxSpeed = 2 #Pixels per second
        self.topRotationSpeed = math.pi / 2 #Radians rotated per second. 2*Pi = 360 degrees
        self.waypoints = []
        self.currentTarget = None
        self.selected = False
        
        
    def drawMinion(self):
        diameter = self.radius * 2
        minionSurface = pygame.Surface((diameter, diameter))
        minionSurface.set_colorkey((0,0,0))
        pygame.draw.circle(minionSurface,
                            (self.team.getTeamColour()),#Red
                           #(self.radius, self.radius),
                           (self.radius, self.radius),
                           self.radius,
                           0)
        pygame.draw.line(minionSurface,
                         (255,255,255),
                         (self.radius, self.radius),
                         (self.radius + (self.radius * self.currentHeading[0]),
                           self.radius + (self.radius * self.currentHeading[1])),
                         1)
        return minionSurface
    
    #Draw the minion to the screen
    def render(self, screen):
        toRender = self.drawMinion()   
        #screen.blit(toRender, (self.position[0] - toRender.get_width() / 2,
       #                             self.position[1] - toRender.get_height() / 2))
        drawPosition = (self.position[0] - self.radius,
                        self.position[1] - self.radius)
        screen.blit(toRender, drawPosition)
        
        #Draw a line from the minion to its target
        if self.currentTarget <> None:
            pygame.draw.line(screen,
                         (155,155,255),
                         (self.position), (self.currentTarget),1)
        
        if self.selected:
            pygame.draw.rect(screen,
                             (255,255,255), 
                             (self.position[0] - self.radius,
                              self.position[1] - self.radius,
                              self.radius * 2,
                              self.radius * 2), 1)
        
        #Draw lines between all the waypoints in the queue
        self.lastWaypoint = ()
        for wayPoint in self.waypoints:
            if self.lastWaypoint is not ():
                pygame.draw.line(screen,
                                 (0,255,0),
                                 (self.lastWaypoint), 
                                 (wayPoint),
                                 1)
            else: #Draw a line from the current target to
                    #the start of the waypoint queue
                pygame.draw.line(screen,
                 (255,255,0),
                 (self.currentTarget), 
                 (wayPoint),
                 1)
            self.lastWaypoint = wayPoint
  
    #Update the minion, based on the there having been msLapsed milliseconds since
    #the last update.
    def update(self, msLapsed):       
        #move = MatrixUtils.multiplyMatrix(msLapsed, self.currentMovement)       
        if self.currentTarget <> None:
            #TODO - Do the following three lines with matrix calculations
            offSetToTarget = MatrixUtils.subtractMatrix(self.currentTarget, self.position)
            distanceToTarget = (offSetToTarget[0]**2 + offSetToTarget[1]**2)**.5
            normalisedToTarget = ((offSetToTarget[0]/distanceToTarget),
                                  (offSetToTarget[1]/distanceToTarget))
            
            #Real Code
            #Move based on how long since the last update. This
            #keeps movement steady over slower/faster updates.
            self.currentMovement = MatrixUtils.multiplyMatrix(msLapsed, normalisedToTarget)
            self.currentMovement = MatrixUtils.multiplyMatrix(.25, self.currentMovement)
            self.position = MatrixUtils.addMatrix(self.position, self.currentMovement)
                           
            if distanceToTarget < 5:
                if len(self.waypoints) > 0:
                    self.currentTarget = self.waypoints.pop(0)
                else:
                    self.currentTarget = None

    def minionAt(self, position):
        #Looking at minion square
        minionRect = pygame.Rect(self.position[0] - self.radius,
                                 self.position[1] - self.radius,
                                 self.radius * 2,
                                 self.radius * 2)
        return minionRect.collidepoint(position[0], position[1])
    
    def select(self):
        self.selected = True
    
    def unselect(self):
        self.selected = False
        
    
    def setTarget(self, targetPos):
        self.currentTarget = targetPos
        
    def addWaypoint(self, targetPos):
        if self.currentTarget == None:
            self.setTarget(targetPos)
        else:
            self.waypoints.append(targetPos)

